﻿

namespace Orid.Management.SOHO.Book.Control.MPK_Calendar
{
    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;

    /// <summary>
    /// 预约时间选择控件
    /// </summary>
    public partial class Timeslotpicker : UserControl
    {
        public Timeslotpicker()
        {
            InitializeComponent();
            ChangeRadioLanguage();

        }
      
        /// <summary>
        /// 保存当前日期
        /// </summary>
        private DateTime CurrentDate { get; set; }

        /// <summary>
        /// 更改显示语言
        /// </summary>
        private void  ChangeRadioLanguage()
        { 

            checkBoxButtonMidnight.Text = ((EBookPciker)1).ToString();
            checkBoxButtonMidnight.Tag = EBookPciker.深夜;

            checkBoxDayBreak.Text = ((EBookPciker)2).ToString();
            checkBoxDayBreak.Tag = EBookPciker.早朝;

            checkBoxButtonMorning.Text = ((EBookPciker)4).ToString();
            checkBoxButtonMorning.Tag = EBookPciker.午前;

            checkBoxButtonAfternoon.Text = ((EBookPciker)8).ToString();
            checkBoxButtonAfternoon.Tag =EBookPciker.午後;

            checkBoxButtonNight.Text = ((EBookPciker)16).ToString();
            checkBoxButtonNight.Tag = EBookPciker.夜;

        }

     
  
        /// <summary>
        /// 设定限制日期范围
        /// 在限定日期之外 此功能不可使用
        /// 所有按钮不可用
        /// </summary>
        public DateTime LimitEnableDateFrom;

        /// <summary>
        /// 设定限制日期范围
        /// 在限定日期之外 此功能不可使用
        /// 所有按钮不可用
        /// </summary>
        public DateTime LimitEnableDateTo; 
 
        /// <summary>
        /// 预约的开始时间
        /// 年月日 时间
        /// </summary>
        public List<BookTime> UnScheduleData
        {
            get
            {
                return GetSelectScheduleData(false);
            }
        }

        /// <summary>
        /// Gets the schedule data.
        /// </summary>
        /// <value>The schedule data.</value>
        public List<BookTime> ScheduleData
        {
            get 
            {
                return GetSelectScheduleData(true);
            }
        }

        /// <summary>
        /// Gets the select schedule data.
        /// </summary>
        /// <param name="check">if set to <c>true</c> [check].</param>
        /// <returns></returns>
        private List<BookTime> GetSelectScheduleData(bool check)
        {
            List<BookTime> bookTimes = null;

            foreach (object control in Controls[0].Controls)
            {
                if (control is System.Windows.Forms.CheckBox)
                {
                    CheckBox checkBox = (CheckBox)control;

                    if (checkBox.Enabled  && checkBox.Checked == check)
                    {
                        if (bookTimes == null)
                        {
                            bookTimes = new List<BookTime>();
                        }

                        //预订的枚举 
                        int picker = (int)((CheckBox)control).Tag;

                        BookTime bookTime = BookPicker.GetBookTime(picker);

                        //当前日期 + 预订时间
                        bookTimes.Add(
                            new BookTime()
                                {
                                    From = new DateTime(CurrentDate.Year, CurrentDate.Month, CurrentDate.Day, bookTime.From.Hour, bookTime.From.Minute, bookTime.From.Second),
                                    To = new DateTime(CurrentDate.Year, CurrentDate.Month, CurrentDate.Day, bookTime.To.Hour, bookTime.To.Minute, bookTime.To.Second)
                                });
                    }
                }
            }

            return bookTimes;
        }

        /// <summary>
        /// 全部 按钮，可以使用，不可以使用
        /// </summary>
        /// <param name="enable"></param>
        private void SetEnable(bool enable)
        {
            if (enable)
            {
               
               checkBoxButtonMorning.Checked =
               checkBoxButtonAfternoon.Checked =
               checkBoxButtonNight.Checked =
               checkBoxButtonMidnight.Checked = 
               checkBoxDayBreak.Checked = false;

               checkBoxButtonMorning.Enabled =
               checkBoxButtonAfternoon.Enabled =
               checkBoxButtonNight.Enabled =
               checkBoxButtonMidnight.Enabled =
               checkBoxDayBreak.Enabled =  true;
            }
            else
            {
                checkBoxButtonMorning.Checked =
               checkBoxButtonAfternoon.Checked =
               checkBoxButtonNight.Checked =
               checkBoxButtonMidnight.Checked =
               checkBoxDayBreak.Checked = 

                checkBoxButtonMorning.Enabled =
                checkBoxButtonAfternoon.Enabled =
                checkBoxButtonNight.Enabled =
                checkBoxButtonMidnight.Enabled =
                checkBoxDayBreak.Enabled = false;
            }
        }

        /// <summary>
        /// 预约之前设置可以选择的被选项
        /// 如果当前是12点，只能预约17点以后的
        /// </summary>
        /// <param name="date"></param>
        public void SetTimeslot(DateTime date)
        {
            CurrentDate = date;

            if ((LimitEnableDateFrom != DateTime.MinValue &&
                               LimitEnableDateTo != DateTime.MinValue) &&
                         (date < LimitEnableDateFrom.Date || date > LimitEnableDateTo.Date))
            {
                SetEnable(false);
            }
            else
            {
                SetEnable(true);

                //如果是一天
                int result = date.Date.CompareTo(DateTime.Now.Date);

                if (result == 0)
                {
                    CheckBox[] checkButtons = new CheckBox[]
                                                  {
                                                      checkBoxButtonMidnight, 
                                                      checkBoxDayBreak,
                                                      checkBoxButtonAfternoon, 
                                                      checkBoxButtonMorning,
                                                      checkBoxButtonNight, 

                                                  };

                    foreach (CheckBox checkButton in checkButtons)
                    {
                        BookTime bookTime = BookPicker.GetBookTime( (int)checkButton.Tag);

                            if (bookTime.From.Hour > date.Hour)
                            {
                                checkButton.Enabled = true;
                            }
                            else
                            {
                                checkButton.Checked = false;
                                checkButton.Enabled = false;
                            }
                        
                    }
                  
                }
                else if (result > 0)
                {
                    SetEnable(true);
                }
                else
                {
                    SetEnable(false);

                }
            }

        }

        /// <summary>
        /// 设置已经预约的时间
        /// </summary>
        /// <param name="bookTimes">The book times.</param>
        public void SetAppointmentTime(BookTime[] bookTimes)
        {

            EBookPciker eBookPciker = BookPicker.GetBookPicker(bookTimes);
            if ((eBookPciker & EBookPciker.深夜) == (EBookPciker)1)
            {
                checkBoxButtonMidnight.Checked = true;
            }

            if ((eBookPciker & EBookPciker.早朝) == (EBookPciker)2)
            {
                checkBoxDayBreak.Checked = true;
            }

            if ((eBookPciker & EBookPciker.午前) == (EBookPciker)4)
            {
                checkBoxButtonMorning.Checked = true;
            }

            if ((eBookPciker & EBookPciker.午後) == (EBookPciker)8)
            {
                checkBoxButtonAfternoon.Checked = true;
            }

            if ((eBookPciker & EBookPciker.夜) == (EBookPciker)16)
            {
                checkBoxButtonNight.Checked = true;
            }

        }
    }
}
